#pragma once

#include "System.h"
#include <map>
#include "Geometry.h"
#include "Matrix4.h"
#include "Sprite.h"
#include "Movable.h"

typedef IDirect3DTexture9 Texture;
typedef IDirect3DVertexBuffer9 VertexBuffer;
typedef std::map<std::string, Texture*> TextureMap;
typedef std::map<unsigned, Sprite*> SpriteMap;

enum GameObjectType; // forward declaration of game object types

class Graphics : public System
{
public:
	Graphics(HWND hWnd, int clientWidth, int clientHeight);
	virtual void Load(); // load assets, etc.
	virtual void ReceiveMessage(Message* msg);
	virtual void Update(float frameTime); // update transformations, draw everything
	virtual void Unload(); // unload assets
	Sprite * AddSprites(string pat); //Adds sprites
	Sprite * UpdateSprites();
	~Graphics();

	Vector2D ScreenToWorld(Vector2D pos); // converts a position vector from screen to world space
private:
	void InitD3D(); // initialize Direct3D
	void LoadTexture(std::string filename);
	void LoadAssets();
	void SetupMatrices();
	void InitGeometry();
	void Render(float frameTime);
	void DrawBackground();
public:
	friend class Sprite;
private:
	HWND hWnd; // window handle
	int ScreenWidth, ScreenHeight; // Screen size
	Vector2D SurfaceSize; // Size of the drawing surface

	// Direct3D pointers
	IDirect3D9* pD3D;
	IDirect3DDevice9* pDevice;
	D3DPRESENT_PARAMETERS PresentParams;

	VertexBuffer* pSpriteBuffer;		 // for regular sprites
	VertexBuffer* pBackgroundBuffer; // for background layers
	VertexBuffer* pParticleBuffer;	 // for drawing particle effects

	TextureMap Textures;  // Textures for all game objects

	SpriteMap Sprites; // List of sprite components that belong to game objects
	//Camera* pCamera;		// Current camera object -- determines viewing params
	Vector3D CameraPos;

	// Projection and view matrices
	Matrix4 ProjMatrix;
	Matrix4 ViewMatrix;
	Matrix4 ViewProjMatrix;
};